package com.atguigu.hashtab;

import java.util.Scanner;

public class HashTab {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab();
        Scanner s = new Scanner(System.in);
        while (true){
            System.out.println("请输入操作方法:");
            String next = s.next();
            switch (next){
                case "add":
                    System.out.println("输入id：");
                    int idd = s.nextInt();
                    System.out.println("输入名字：");
                    String namee = s.next();
                    Emp emp = new Emp(idd, namee);
                    hashTab.add(emp);
                    break;

                case "list":
                    hashTab.list();
            }
        }


    }
    int size = 7;
    LinkEmp[] lp = new LinkEmp[size];

    public HashTab(){
        for (int i = 0; i < lp.length; i++) {
            lp[i] = new LinkEmp();
        }
    }

    public int getNo(int id){
         int no = id % size;
         return no;
    }

    public void add(Emp emp){
        int aid = emp.getId();
        lp[getNo(aid)].add(emp);
    }

    public void list(){
        for (int i = 0; i < lp.length; i++) {
            System.out.println("打印第"+(i + 1)+":");
            lp[i].list();
        }
    }
}

class Emp {
    private int id;
    private String name;
    private Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Emp getNext() {
        return next;
    }

    public void setNext(Emp next) {
        this.next = next;
    }
}

class LinkEmp {
    private Emp head;

    public Emp getHead() {
        return head;
    }

    public void setHead(Emp head) {
        this.head = head;
    }

    public void add(Emp emp){
        if (head == null){
            head = emp;
            return;
        }

        Emp tempEmp = head;
        while (true){
            if (tempEmp.getNext() == null){
                tempEmp.setNext(emp);
                return;
            }
            tempEmp = tempEmp.getNext();
        }
    }

    public void list(){
        if (head == null){
            return;
        }else {
            System.out.printf("id=%d name=%s \n",head.getId() ,head.getName());
        }
        Emp tempEmp = head.getNext();
        while (true){
            if (tempEmp==null){
                return;
            }else {
                System.out.printf("id=%d name=%s \n",tempEmp.getId(),tempEmp.getName());
            }
            tempEmp = tempEmp.getNext();
        }
    }
}
